package com.ld.cloud.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

@Configuration
public class RestConfig {

    /**
     * @LoadBalanced : 负载均衡，调用eurake server上的服务需要在template上指定负载均衡的方式
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTempate(){
        return new RestTemplate();
    }

    /**
     * 添加一个Http头信息配置，配置下认证信息
     * @return
     */
    @Bean
    public HttpHeaders getHeader(){
        HttpHeaders httpHeaders = new HttpHeaders();
        String auth = "user:user";
        byte[] encodeAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
        String authHeader = "Basic"+new String(encodeAuth);
        httpHeaders.set("Authorization",authHeader);
        return httpHeaders;
    }

    /**
     *  配置全局访问策略
     * @return
     */
//    @Bean
//    public IRule iRule(){
//        return new RandomRule();//随机访问
//    }
}
